<?php

  defined('ACC') || exit('Acc defined');

  class orderGoodsModel extends Model{
      protected $table = 'ordergoods';
      protected $pk = 'og_id';
      public $fields = [
          'og_id','order_id','order_sn','goods_id','goods_name','goods_number','shop_price', 'subtotal'
      ];

      public $rule = [
          ['order_id',1,'订单ID不能为空','numeric',''],
          ['order_sn',1,'订单编号不能为空或重复','unique',''],
          ['goods_id',1,'商品ID不能为空','numeric',''],
          ['goods_number',1,'商品数量不能为空','required',''],
          ['shop_price',1,'商品售价不能为空','required',''],
      ];

      //添加订单对应的商品详情表
      public function addGoods($data){
          if($this->add($data)){   //添加单条订单商品详情数据
              //添加成功后，修改商品库存
              $sql = 'update goods set goods_number = goods_number - ' . $data['goods_number']
                  . ' ,sell_number =' . ' sell_number + ' .$data['number']
                  . ' where goods_id = ' . $data['goods_id'];
              $this->db->query($sql);
              return true;
          }
          return false;
      }

      //回滚

  }
